package com.itheima.tanhua.server.controller;

import com.itheima.tanhua.common.utils.JwtUtils;
import com.itheima.tanhua.po.LoginPo;
import com.itheima.tanhua.po.LoginReginfoPo;
import com.itheima.tanhua.po.PhonePo;
import com.itheima.tanhua.server.service.UserService;
import com.itheima.tanhua.vo.LoginVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author itheima
 * @since 2022-01-13
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     *
     * @param phonePo phonePo
     * @return ResponseEntity
     */
    @PostMapping("login")
    public ResponseEntity sendMessage(@RequestBody PhonePo phonePo) {
        userService.sendMessage(phonePo.getPhone());
        return ResponseEntity.ok(null);
    }

    /**
     * 登录
     *
     * @param loginPo LoginPo
     * @return ResponseEntity
     * @throws Exception Exception
     */
    @PostMapping("loginVerification")
    public ResponseEntity loginVerification(@RequestBody LoginPo loginPo) throws Exception {
        String phone = loginPo.getPhone();
        String verifyCode = loginPo.getVerificationCode();
        LoginVo loginVo = userService.loginVerification(phone, verifyCode);
        return ResponseEntity.ok(loginVo);
    }

    /**
     * Day01 保存用户信息
     *
     * @param po LoginReginfoPo
     * @return ResponseEntity
     */
    @PostMapping("loginReginfo")
    public ResponseEntity loginReginfo(LoginReginfoPo po) {
        userService.loginReginfo(po);

        return ResponseEntity.ok(null);
    }

    /**
     * Day01 上传头像
     *
     * @param headPhoto 头像
     * @return ResponseEntity
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto) {
        userService.loginReginfoHead(headPhoto);
        return ResponseEntity.ok(null);
    }
}
